package com.ellricky.excute_president.ui.activity.home.task

import android.content.Intent
import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.dialog.picker.EPickerStrListener
import com.ellricky.baselib.dialog.picker.EPickerType
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.excute_president.*
import kotlinx.android.synthetic.main.pd_activity_task_create.*
import org.json.JSONObject

class TaskCreateActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),
        EPickerStrListener {

    private var pageType: Int = TASK_PAGE_OTHER
    private var taskTitle: String = ""

    private lateinit var ePickerUtil: EPickerUtil
    private var selLinePos = -1

    //  5=课消 6=招新 7=地推 8=其他
    private var postTaskType = ""
    private var postStandardLine = ""


    override fun getContentLayout(): Int {
        return R.layout.pd_activity_task_create
    }

    override fun initUIView() {
        super.initUIView()
        ePickerUtil = EPickerUtil(this)
        mTvTitle.text = getString(R.string.pd_task_create)

        pageType = intent.getIntExtra(KEY_PAGE_TYPE, TASK_PAGE_OTHER)
        taskTitle = intent.getStringExtra(KEY_TASK_TITLE) ?: ""
        etType.value = taskTitle
        etType.hideNoInoutIcon()

        etAttend.initDataWithNull(false)

        visibility()
    }

    private fun visibility() {
        when (pageType) {
            TASK_PAGE_KX -> {
                etSaleCount.visibility = View.GONE
                etAddress.visibility = View.GONE
                etLine.setOnClickListener(this)

                postTaskType = "5"
            }
            TASK_PAGE_ZX -> {
                etHour.visibility = View.GONE
                etAddress.visibility = View.GONE
                etScoreRemove.visibility = View.GONE

                etLine.initEditable(true)
                etLine.setKey(getString(R.string.pd_line_title_yuan))
                etLine.setHint(getString(R.string.pd_task_line_input))
                etLine.setInputNumber()
                etLine.hideNoInoutIcon()

                postTaskType = "6"
            }
            TASK_PAGE_DT -> {
                etHour.visibility = View.GONE
                etSaleCount.visibility = View.GONE
                etLine.visibility = View.GONE

                postTaskType = "7"
            }
            else -> {
                etHour.visibility = View.GONE
                etSaleCount.visibility = View.GONE
                etLine.visibility = View.GONE
                etScoreRemove.visibility = View.GONE

                postTaskType = "8"
            }
        }

        etHour.setInputNumber()
        etScoreAdd.setInputNumber()
        etScoreRemove.setInputNumber()

        etStartDate.setOnClickListener(this)
        etEndDate.setOnClickListener(this)


        btnSubmit.setOnClickListener(this)


    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            etStartDate -> {
                val startDate = etStartDate.value
                ePickerUtil.showStartDatePicker(startDate, this)
            }
            etEndDate -> {
                val endDate = etEndDate.value
                ePickerUtil.showEndDatePicker(endDate, this)
            }
            etLine -> {
                ePickerUtil.showLinePicker(selLinePos, this)
            }
            btnSubmit -> postSubmit()
        }
    }

    override fun onSelectItem(content: String?, picPos: Int, pickType: Int) {
        when (pickType) {

            EPickerType.PICK_START_DATE -> {
                etStartDate.value = content
            }
            EPickerType.PICK_END_DATE -> {
                etEndDate.value = content
            }
            EPickerType.PICK_LINE -> {
                selLinePos = picPos
                etLine.value = content
            }
        }
    }


    private fun postSubmit() {
        val taskType = etType.value
        if (EmptyUtil.isEmpty(taskType)) {
            ToastUtil.show(this, R.string.pd_task_select_type)
            return
        }

        val title = etTile.value
        if (EmptyUtil.isEmpty(title)) {
            ToastUtil.show(this, R.string.pd_task_input_title)
            return
        }

        val startDate = etStartDate.value
        val endDate = etEndDate.value
        val timeCheck = (startDate ?: "")
                .compareTo(endDate ?: "", true) > 0
        if (EmptyUtil.isEmpty(startDate)) {
            ToastUtil.show(this, R.string.pd_select_start_time)
            return
        }
        if (EmptyUtil.isEmpty(endDate)) {
            ToastUtil.show(this, R.string.pd_select_end_time)
            return
        }
        if (timeCheck) {
            ToastUtil.show(this, R.string.pd_select_compare_time)
            return
        }

        val classNum = etHour.value
        val giveScore = etScoreAdd.value
        val taskInfo = etContent.value
        val attendIds = etAttend.ids


        val removeScore = etScoreRemove.value
        val address = etAddress.value
        val standardLine = etLine.value
        val sellPrice = etSaleCount.value

        val paramMap = mutableMapOf<String, Any>(
                "taskType" to postTaskType,
                "title" to title,
                "startDate" to startDate,
                "endDate" to endDate,
                "giveScore" to giveScore,
                "taskInfo" to taskInfo,
                "attendIds" to attendIds
        )

        when (pageType) {
            TASK_PAGE_KX -> {
                if (EmptyUtil.isEmpty(classNum)) {
                    ToastUtil.show(this, R.string.pd_hour_count_input)
                    return
                }
                paramMap["classNum"] = classNum
            }
            TASK_PAGE_ZX -> {
                if (EmptyUtil.isEmpty(sellPrice)) {
                    ToastUtil.show(this, R.string.pd_sale_count_input)
                    return
                }
                paramMap["sellPrice"] = sellPrice
            }
            TASK_PAGE_DT,
            TASK_PAGE_OTHER -> {
                if (EmptyUtil.isEmpty(address)) {
                    ToastUtil.show(this, R.string.pd_address_input)
                    return
                }
                paramMap["address"] = address
            }
        }

        if (EmptyUtil.isEmpty(giveScore)) {
            ToastUtil.show(this, R.string.pd_task_input_score)
            return
        }


        when (pageType) {
            TASK_PAGE_KX -> {
                if (EmptyUtil.isEmpty(removeScore)) {
                    ToastUtil.show(this, R.string.pd_task_input_score2)
                    return
                }
                if (EmptyUtil.isEmpty(standardLine)) {
                    ToastUtil.show(this, R.string.pd_task_select_line)
                    return
                }
                postStandardLine = standardLine.replace("%", "")

                paramMap["removeScore"] = removeScore
                paramMap["standardLine"] = postStandardLine
            }
            TASK_PAGE_ZX -> {
                if (EmptyUtil.isEmpty(standardLine)) {
                    ToastUtil.show(this, R.string.pd_task_line_input_please)
                    return
                }
                postStandardLine = standardLine
                paramMap["standardLine"] = postStandardLine
            }

            TASK_PAGE_DT -> {
                if (EmptyUtil.isEmpty(removeScore)) {
                    ToastUtil.show(this, R.string.pd_task_input_score2)
                    return
                }
                paramMap["removeScore"] = removeScore
            }
        }


        if (EmptyUtil.isEmpty(taskInfo)) {
            ToastUtil.show(this, R.string.pd_task_input_content)
            return
        }
        if (EmptyUtil.isEmpty(attendIds)) {
            ToastUtil.show(this, R.string.pd_task_add_attend)
            return
        }

        val url = EAppUtil.getApiHost() + API_TASK_CREATE
        HttpUtils.getInstance().post(url, paramMap, object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject?, msg: String?) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }

        })

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        EAppUtil.onChosePerson(etAttend, false, resultCode, data)
    }

}

